VFG.EditPModel=function(viewer,entity){
	this.viewer=viewer;
	this.tip=new VFG.Tip(viewer);
	this.entity=entity;
	this.position=entity.position||null;
	this.orientation=entity.orientation||{};
	this.model=entity;
	
	this.cartesian3;
	this.rotatePosition;
	this.radius;
	this.init();
}
VFG.EditPModel.prototype.init=function(){
	var _this=this;
	this.cartesian3 = VFG.Util.getLnLaToC3(this.position);
	this.height = Cesium.Cartographic.fromCartesian(this.cartesian3).height;
    this.radius = _this.entity.getRadius();
	_this.ellipseEntity = this.viewer.entities.add({
        position: new Cesium.CallbackProperty(function (time) {
            return _this.cartesian3;
        }, false),
        ellipse:{
            fill: false,
            outline: true,
            outlineColor: Cesium.Color.YELLOW,
            outlineWidth:2,
            semiMajorAxis: new Cesium.CallbackProperty(function (time) {
                return _this.radius;
            }, false),
            semiMinorAxis:new Cesium.CallbackProperty(function (time) {
                return _this.radius;
            }, false),
            height:  new Cesium.CallbackProperty(function (time) {
                return Cesium.Cartographic.fromCartesian(_this.cartesian3).height;
            }, false)
        }
    });
	_this.rotatePosition=this.getHeadingPosition();
	
	_this.rotatePoint=_this.viewer.entities.add({
		id:_this.entity.id+'-rotatePoint',
        position: new Cesium.CallbackProperty(function (time) {
            return _this.rotatePosition;
        }, false),
        point:{
            color: Cesium.Color.VIOLET  ,
            pixelSize:10,
        }
    });
	_this.zoomPoint=_this.viewer.entities.add({
		id:_this.entity.id+'-zoomPoint',
        position: new Cesium.CallbackProperty(function (time) {
            return VFG.Util.addPositionsHeight(_this.cartesian3, _this.radius);
        }, false),
        point:{
            color: Cesium.Color.BLUEVIOLET,
            pixelSize:10,
        }
    }); 
	
	this._handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
	var pickEntity;
	
	//鼠标左键按下
	this._handler.setInputAction(function(e) {
		let pick = _this.viewer.scene.pick(e.position);
		if (Cesium.defined(pick) && Cesium.defined(pick.id) && pick.id.id==_this.entity.id+'-rotatePoint'){
			_this.pickEntity=pick;
	 		_this.rotatePoint.point.color=Cesium.Color.YELLOW;
	 		_this.viewer.scene.screenSpaceCameraController.enableRotate = false;//锁定相机
		}
		else if (Cesium.defined(pick) && Cesium.defined(pick.id) && pick.id.id==_this.entity.id+'-zoomPoint'){
			_this.pickEntity=pick;
	 		_this.zoomPoint.point.color=Cesium.Color.YELLOW;
	 		_this.viewer.scene.screenSpaceCameraController.enableRotate = false;//锁定相机
		} 
		else if(Cesium.defined(pick) && Cesium.defined(pick.id)  && pick.id==_this.entity.id){
			_this.pickEntity=pick;
	 		//_this.rotatePoint.point.color=Cesium.Color.YELLOW;
	 		_this.viewer.scene.screenSpaceCameraController.enableRotate = false;//锁定相机
		}
	},Cesium.ScreenSpaceEventType.LEFT_DOWN);
    
	this._handler.setInputAction(function(e) {
		if(_this.pickEntity){
			if(Cesium.defined(_this.pickEntity.id) && _this.pickEntity.id.id==_this.entity.id+'-rotatePoint'){
				_this.rotatePoint.point.color=Cesium.Color.VIOLET;
			}
			else if(Cesium.defined(_this.pickEntity.id) && _this.pickEntity.id.id==_this.entity.id+'-zoomPoint'){
				_this.zoomPoint.point.color=Cesium.Color.BLUEVIOLET;
			}
		}
		_this.viewer.scene.screenSpaceCameraController.enableRotate = true;
		_this.tip.setVisible(false);
		_this.pickEntity=null;
	},Cesium.ScreenSpaceEventType.LEFT_UP);
	
	this._handler.setInputAction(function(movement) {
		let pick = _this.viewer.scene.pick(movement.endPosition);
		if (Cesium.defined(pick) && Cesium.defined(pick.id) && pick.id.id==_this.entity.id+'-rotatePoint'){
			_this.tip.setVisible(true);
			_this.tip.showAt(movement.endPosition,"按住鼠标左键拖动点调整模型方位角。");
		}
		else if (Cesium.defined(pick) && Cesium.defined(pick.id) && pick.id.id==_this.entity.id+'-zoomPoint'){
			_this.tip.setVisible(true);
			_this.tip.showAt(movement.endPosition,"按住鼠标左键拖动点调整模型缩放。");
		}
		else if(Cesium.defined(pick) && Cesium.defined(pick.id)  && pick.id==_this.entity.id){
			_this.tip.setVisible(true);
			_this.tip.showAt(movement.endPosition,"按住鼠标左键可拖动模型位置。");
		}else{
			_this.tip.setVisible(false);
		}
		
		if(_this.pickEntity){
			let endC3=VFG.Util.getScreenToC3(_this.viewer, movement.endPosition, _this.pickEntity);
			if(Cesium.defined(endC3)){
				if(Cesium.defined(_this.pickEntity.id) && _this.pickEntity.id.id==_this.entity.id+'-rotatePoint'){
					_this.orientation.heading=_this.getHeading(_this.cartesian3, endC3);
					_this.rotatePosition=_this.getHeadingPosition();
					_this.entity.updateHeading(_this.orientation.heading)
					if(_this.change){
						_this.change({
							heading:_this.orientation.heading,
						});
					}
					_this.tip.showAt(movement.endPosition,"调整模型方位角中...");
				}
				else if(Cesium.defined(_this.pickEntity.id) && _this.pickEntity.id.id==_this.entity.id+'-zoomPoint'){
                    var radiusNew = Cesium.Cartesian3.distance(endC3, _this.cartesian3);
                    var radiusOld = _this.radius / (_this.model.model.scale||1)*1;
                    var scaleNew = radiusNew / radiusOld;
                    _this.radius = radiusNew;
                    _this.model.model.scale = VFG.Util.formatNum(scaleNew, 2);
                    _this.entity.updateScale(_this.model.model.scale);
					_this.rotatePosition=_this.getHeadingPosition();
					if(_this.change){
						_this.change({
							scale:_this.model.model.scale,
						});
					}
					_this.tip.showAt(movement.endPosition,"调整模型缩放中...");
				}
				else if(_this.pickEntity.id==_this.entity.id){
					_this.cartesian3=endC3;
					_this.rotatePosition=_this.getHeadingPosition();
					let LnLa=VFG.Util.getLnLaFormC3(_this.viewer, endC3);
					_this.entity.updatePosition(LnLa)
					if(Cesium.defined(LnLa)){
						if(_this.change){
							_this.change({
								position:LnLa,
							});
						}
					}
					_this.tip.showAt(movement.endPosition,"调整模型位置中...");
				}
			}
		}
	}, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
	this._handler.setInputAction(function(movement) {
	}, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
	
/*	new VFG.OrbitControls(this.viewer,{
		id:this.entity.id,
		position:this.cartesian3,
		scale:this.radius,
		callback:function(position){
			_this.cartesian3=position;
		}
	})*/
}

VFG.EditPModel.prototype.getHeadingPosition=function(){
    var _this=this;
    var angle = -Number(this.orientation.heading || 0);
    var rotpos = new Cesium.Cartesian3(this.radius, 0.0, 0.0);
    var mat = Cesium.Transforms.eastNorthUpToFixedFrame(this.cartesian3);
    var rotationX = Cesium.Matrix4.fromRotationTranslation(Cesium.Matrix3.fromRotationZ(Cesium.Math.toRadians(angle)));
    Cesium.Matrix4.multiply(mat, rotationX, mat);
    mat = Cesium.Matrix4.getMatrix3(mat, new Cesium.Matrix3());
    rotpos = Cesium.Matrix3.multiplyByVector(mat, rotpos, rotpos);
    rotpos = Cesium.Cartesian3.add(this.cartesian3, rotpos, rotpos);
    return rotpos;
}

VFG.EditPModel.prototype.getHeading=function(positionCenter, positionNew){
    var mat = Cesium.Transforms.eastNorthUpToFixedFrame(positionCenter);
    mat = Cesium.Matrix4.getMatrix3(mat, new Cesium.Matrix3());
    var xaxis = Cesium.Matrix3.getColumn(mat, 0, new Cesium.Cartesian3());
    var yaxis = Cesium.Matrix3.getColumn(mat, 1, new Cesium.Cartesian3());
    var zaxis = Cesium.Matrix3.getColumn(mat, 2, new Cesium.Cartesian3());
    var dir = Cesium.Cartesian3.subtract(positionNew, positionCenter, new Cesium.Cartesian3());
    dir = Cesium.Cartesian3.cross(dir, zaxis, dir);
    dir = Cesium.Cartesian3.cross(zaxis, dir, dir);
    dir = Cesium.Cartesian3.normalize(dir, dir);
    var heading = Cesium.Cartesian3.angleBetween(xaxis, dir);
    var ay = Cesium.Cartesian3.angleBetween(yaxis, dir);
    if (ay > Math.PI * 0.5) {
        heading = 2 * Math.PI - heading;
    }
    return -Cesium.Math.toDegrees(heading);
}

VFG.EditPModel.prototype.on = function(type,callback) {
	if('change'==type){
		this.change=callback;
	}
};

VFG.EditPModel.prototype.update=function(entity){
    var _this=this;
    if(entity){
    	
    	_this.entity=entity;
    	_this.position=entity.position||null;
    	_this.orientation=entity.orientation||{};
    	_this.model=entity;
    	
    	_this.cartesian3 = VFG.Util.getLnLaToC3(_this.position);
    	_this.height = Cesium.Cartographic.fromCartesian(_this.cartesian3).height;
    	_this.radius = _this.entity.getRadius();
    	_this.rotatePosition=_this.getHeadingPosition();
    	
    }
}


VFG.EditPModel.prototype.destroy = function() {
    if(this.ellipseEntity){
    	this.viewer.entities.remove(this.ellipseEntity);
    }
    if(this.rotatePoint){
    	this.viewer.entities.remove(this.rotatePoint);
    }
    if(this.zoomPoint){
    	this.viewer.entities.remove(this.zoomPoint);
    }
    if( this._handler){
    	this._handler.destroy();
    	this._handler = null;
    };
    if(this.tip){
    	this.tip.destroy();
    }
	delete this.ellipseEntity;
	delete this.rotatePoint;
	delete this.zoomPoint;
    delete this.viewer;
	return Cesium.destroyObject(this);
};
